<?php

namespace App\Libraries\Criteria;

use Prettus\Repository\Contracts\CriteriaInterface;
use Prettus\Repository\Contracts\RepositoryInterface;

class parentCriteria implements CriteriaInterface
{
    protected $filed;
    protected $pid;

    public function __construct($filed, $pid)
    {
        $this->filed = $filed;
        $this->pid = $pid;
    }

    public function apply($model, RepositoryInterface $repository)
    {
        $model = $model->where($this->filed, $this->pid);
        if ('' === $this->pid) {
            $model = $model->orWhere($this->filed, 'is', null);
        }

        return $model->orderBy('sort_code');
    }
}
